import BpmInstApi from "@/common/api/bpm/bpmInst";
import { mapState } from 'pinia'
import useMain from "@/stores/useMain";

export default {
    inject:['appId'],
    computed:{
        ...mapState(useMain,['appKey','curApp']),
        //是否时独立打开审批页面还是使用的统一URL打开
        isAlone(){
            return this.$route.name!="openDoc" && !this.newPage;
        }
    },
    data(){
        return {
            newPage:true
        }
    },
    methods:{
        encryptInstId(instId,callback){
            BpmInstApi.getBpmInstEncrypt(instId).then(res=>{
                if(!res.success){
                    this.$message.warn(res.message);
                    return;
                }
                callback(res.data);
            })
        },
        encryptInstIds(instIds,relType,callback){
            BpmInstApi.getBpmInstEncryptList(instIds,relType).then(resultList=>{
                callback(resultList);
            })
        },
        /**
         * 启动流程页面
         * @param defKey
         */
        startProcess(defKey,appId){
            let params={
                defKey:defKey,
                appKey:this.appKey
            };

            let obj=this.$router.resolve({
                name:'newDoc',
                params:params,
                query:{appId:appId}
            });
            let href=obj.href ;

            window.open(href,"_blank" );
        },
        /**
         * 打开流程实例页面
         * <pre>
         *     1.审批页面
         *     2.流程实例
         * </pre>
         * @param instId 示例ID
         * @param taskId 任务ID
         * @param taskType
         */
        openInst(appId,instId,taskId,from){
            var self=this;
            self.encryptInstId(instId,function(encryptInstId){
                appId=encryptInstId.appId;
                encryptInstId=encryptInstId.instId;
                let query={appId:appId};
                if(taskId){
                    query.taskId=taskId;
                }
                if(from){
                    query.from=from;
                }
                let obj=self.$router.resolve({
                    name:'openDoc',
                    params:{instId:encryptInstId,appKey:self.appKey},
                    query:query
                });
                window.open(obj.href,"_blank" );
            })
        },
        openDetail(appId,instId,from){
            this.openInst(appId,instId,null,from);
        },
        /**
         * 打开流程实例页面
         * <pre>
         *     1.审批页面
         *     2.流程实例
         * </pre>
         * @param instId 示例ID
         * @param appId 应用ID
         */
        openOperator(record){
            var self=this;
            self.encryptInstId(record.instId,function(encryptInstId){
                let appId=encryptInstId.appId;
                encryptInstId=encryptInstId.instId;
                let params={instId:encryptInstId,appKey:self.appKey};
                let obj=self.$router.resolve({
                    name:'openDoc',
                    params:params,
                    query:{
                        defId:record.defId,
                        instId:record.instId,
                        appId:appId,
                        operator:"operator"
                    }
                });
                window.open(obj.href,"_blank" );
            })
        },
        pushInst(instId,appId){
            var self=this;
            this.encryptInstId(instId,function(encryptInstId) {
                encryptInstId=encryptInstId.instId;
                let time = new Date().getTime();
                var query={time:time};
                if(appId){
                    query.appId=appId;
                }
                self.$router.push({
                    name:'openDoc',
                    params:{instId:encryptInstId,appKey:self.appKey},
                    query:query
                });
            });
        },
        copyInst(instId){
            let params={instId:instId,appKey:this.appKey};
            let obj=this.$router.resolve({
                name:'openDoc',
                params:params
            });
            let url=obj.href +"?action=copy";
            window.open(url,"_blank" );
        },
        executeMethod(method,data){
            let rxForms=this.$refs.rxForms;
            if(!rxForms){
                return;
            }
            rxForms.executeMethod(method,data);
        }
    }
}
